package com.museum.monitor;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.museum.counst.USERS;
import com.museum.entities.action.ActionResponse;
import com.museum.entities.friend.Friend;
import com.museum.entities.friend.FriendList;
import com.museum.entities.process.MakeList;
import com.museum.entities.process.ProcessItem;
import com.museum.entities.process.ProcessedItem;
import com.museum.entities.process.ProcessingItem;
import com.museum.entities.userinfo.UserData;
import com.museum.utils.Sentor;
import com.museum.utils.Utils;

public class MakeMonitor {
	private static final Log log = LogFactory.getLog(MakeMonitor.class);
	private static final Sentor sentor = new Sentor();
	private static final String MYID = USERS.uids[0];
	private static final int target_price = 300;

	public static void main(String[] args) {
		FriendList fl = sentor.friends();
		if (Utils.isBlank(fl.getErr())) {
			for (int i = 0; i < fl.getData().getTotal(); i++) {
				Friend friend = fl.getData().getList().get(i);
				String uid = friend.getUid();
				UserData ud = sentor.friendInfo(uid).getData();
				log.info(ud.getName());
				MakeList ml = sentor.makeList(uid);
				List<ProcessedItem> pdList = ml.getData().getProcessed();
				List<ProcessingItem> pgList = ml.getData().getProcessing();
				List<ProcessItem> pList = ml.getData().getList();
				for (int pdi = 0; pdi < pdList.size(); pdi++) {
					ProcessedItem pditem = pdList.get(pdi);
					if (pditem.getUid().equals(MYID)) {
						ActionResponse ar = sentor.makeDone(
								pditem.getItem_id(), uid);
						log.info(ar.getData().getText());
					}
				}
				if (pgList.size() == 0 || MYID.equals(uid)) {
					for (int pi = 0; pi < pList.size(); pi++) {
						ProcessItem pitem = pList.get(pi);
						int state = pitem.getState();
						String desc = pitem.getDescription();
						if (desc.endsWith(target_price + "金币。") && state == 0) {
							ActionResponse ar = sentor.make(pitem.getItem_id(), uid);
							log.info(ar.getData().getText());
						}
					}
				}

			}
		} else {
			log.error(fl.getErr());
		}
	}
}
